#!/bin/sh
# Copyright (C) 2022 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

: "${MK_INSTALLDIR:=""}"
: "${MK_CONFDIR:="/etc/check_mk"}"

if [ -n "${MK_INSTALLDIR}" ]; then
    CONFIG="${MK_INSTALLDIR}/package/config/super-server.cfg"
else
    CONFIG="${MK_CONFDIR}/super-server.cfg"
fi

MODULES_DIR="$(dirname "$(realpath -- "$0" || printf "%s" "$0")")"

usage() {
    cat >&2 <<HERE
Usage ${0} deploy|cleanup|purge|trigger|getdeployed

Commands:
  deploy       Deploy the files for the (first available) configured
               super server.
  cleanup      Cleanup all super server files
  purge        Cleanup all super server files that may remain from old agent installations
  trigger      Reload or start all super servers
  getdeployed  Print the deployed super server
HERE
    exit 1
}

_all_failed() {
    cat >&2 <<HERE
---------------------------------------------
WARNING

This package comes with configuration files
for the following super server(s):
$(_available_modules '  ')
It is configured to try these (in order of
preference):
$(_configured_modules '  ')
None of these have been applicable.

Hint: It's also possible to call the
Checkmk agent via SSH without a running
agent service.
---------------------------------------------
HERE
}

_module_subfolders() {
    find "${MODULES_DIR}" -type d -name "*_*"
}

_available_modules() {
    _module_subfolders | while read -r name; do
        bname="$(basename "${name}")"
        printf "%s%s\n" "$1" "${bname#*_}"
    done | sort -n
}

_configured_modules() {
    lines="$(grep -v '^#' "${CONFIG}" 2>/dev/null)" || lines="auto"
    case "$lines" in
        *auto* | "")
            _available_modules "$1"
            ;;
        *no_service*)
            return
            ;;
        *)
            for name in $lines; do
                printf "%s%s\n" "$1" "${name}"
            done
            ;;
    esac
}

cleanup() {
    _module_subfolders | while read -r module; do
        /bin/sh "${module:?}/setup" cleanup
    done
}

purge() {
    _module_subfolders | while read -r module; do
        /bin/sh "${module:?}/setup" purge
    done

    cleanup
}

deploy() {
    _configured_modules | while read -r module; do
        module_path="$(find "${MODULES_DIR}" -type d -name "*${module}" | head -n1)" || continue
        /bin/sh "${module_path:?}/setup" deploy && {
            echo "Deployed ${module}"
            return 0
        }
    done || _all_failed
}

trigger() {
    # first trigger not deployed ones, to free resources (e.g. port)
    _module_subfolders | while read -r module; do
        /bin/sh "${module:?}/setup" isdeployed || /bin/sh "${module}/setup" trigger
    done
    # then trigger deployed ones
    _module_subfolders | while read -r module; do
        /bin/sh "${module:?}/setup" isdeployed && /bin/sh "${module}/setup" trigger
    done

    return 0
}

getdeployed() {
    _module_subfolders | while read -r module; do
        /bin/sh "${module:?}/setup" isdeployed || continue
        bname="$(basename "${module}")"
        printf "%s\n" "${bname#*_}"
    done
}

main() {
    [ "$1" = "-v" ] && {
        shift
        set -x
    }

    case "$1" in
        deploy)
            deploy
            ;;
        cleanup)
            cleanup
            ;;
        purge)
            purge
            ;;
        trigger)
            trigger
            ;;
        getdeployed)
            getdeployed
            ;;
        *)
            usage
            ;;
    esac
}

[ -z "$MK_SOURCE_ONLY" ] && main "$@"
